<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/4/2 20:52
 * @version
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Advs extends Model
{
    protected $table = 'advs';

    protected $guarded = [];

    public $appends = [];

    public function image()
    {
        return $this->belongsTo(Attachments::class, 'image_id', 'id');
    }

    public static function advsImage($location,$way='id',$desc='asc'){
        $data = self::where('location',$location)->where('status',1)->orderBy($way,$desc)->with('image')->get();
        $array = [];
        if ($data->count()>0){
            foreach ($data as $item){
                $array[] = [
                    'id'          => $item->id,
                    'image'       => $item->image->file_url,
                    'width'       => $item->image->width,
                    'height'      => $item->image->height,
                    'redirect_type' => $item->redirect_type,
                    'redirect_to' => $item->redirect_to,
                    'url'=>$item->url,
                ];
            }
        }
        return $array;
    }

}